//
//  PuzzleViewController.m
//  Puzzle_Crazy
//
//  Created by Le Hung on 3/31/14.
//  Copyright (c) 2014 DanielLe. All rights reserved.
//

#import "PuzzleViewController.h"
#import "IXNTilePuzzle.h"
#import "DBManager.h"
#import "SimpleAudioEngine.h"
#import "Animations.h"
#import "PuzzleObject.h"
#import "UIButton+Block.h"
#import "ResultView.h"
#import "KGModal.h"

static const NSTimeInterval AnimationSpeed = 2.0;
@interface PuzzleViewController ()<IXNTileBoardViewDelegate>
{
    PuzzleObject *puzzleObj;
}

@end

@implementation PuzzleViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    puzzleObj = [[PuzzleObject alloc] init];
    [self showImage];
    [KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeNone;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showImage
{
    self.imgViewPreview.image = [UIImage imageNamed:self.strImage];
    [self performSelector:@selector(animation) withObject:self afterDelay:1.0f];
    
}

- (void)animation
{
    self.puzzleView.alpha = 0.2;
    self.imgViewPreview.transform = CGAffineTransformMakeScale(2, 2);
    [UIView animateWithDuration:1.5 animations:^{
        self.imgViewPreview.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        self.puzzleView.alpha = 1;
        [self restart:nil];
    }];
}

- (void)hideImage
{
    if (!self.imgProduct) return;
    
    [UIView animateWithDuration:AnimationSpeed animations:^{
        self.imgProduct.alpha = 0.0;
        self.puzzleView.alpha = 1.0;
    } completion:^(BOOL finished) {
        [self.imgProduct removeFromSuperview];
        self.imgProduct = nil;
    }];
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


#pragma mark - Action Button
- (IBAction)restart:(id)sender {
    self.imgProduct.image = [UIImage imageNamed:self.strImage];
    [self.puzzleView playWithImage:self.imgProduct.image size:self.level];
    [self.puzzleView shuffleTimes:100];
    self.steps = 0;
}

- (IBAction)exit:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)music:(UIButton *)sender {
    if (!sender.isSelected) {
        [sender setSelected:YES];
    } else
    {
        [sender setSelected:NO];
    }
    
}

- (IBAction)preview:(UIButton *)sender {
    if (!sender.isSelected) {
        [sender setSelected:YES];
        self.puzzleView.alpha = 0.75;
    } else
    {
        [sender setSelected:NO];
        self.puzzleView.alpha = 1;
    }
    
}


#pragma mark - Tile Board Delegate Method
- (void)tileBoardView:(IXNTileBoardView *)tileBoardView tileDidMove:(CGPoint)position
{
    NSLog(@"tile move : %@", [NSValue valueWithCGPoint:position]);
    self.steps++;
}

- (void)tileBoardViewDidFinished:(IXNTileBoardView *)tileBoardView
{
    NSLog(@"tile is completed: %d",self.steps);
    puzzleObj.Objimage = self.strImage;
    if (self.level == levelEasy) {
        puzzleObj.Objeasy = [NSString stringWithFormat:@"%d",self.steps];        
    } else if (self.level == levelMedium) {
        puzzleObj.Objmedium = [NSString stringWithFormat:@"%d",self.steps];
    } else if (self.level == levelHard) {
        puzzleObj.Objhard = [NSString stringWithFormat:@"%d",self.steps];
    }
    [[DBManager shareInstance] updateLevelData:puzzleObj];
    [self showResultView];
}

- (void)showResultView
{
    CGPoint center = self.view.center;
    ResultView *resultView = [[ResultView alloc] init];
    __weak ResultView *resultView_ = resultView;
    resultView_.frame = CGRectMake(25, 0, 270, 170);
    [resultView_ setCenter:center];
    
    /* star */
    if (self.steps >= k1StarEasy) {
        // 1 star
        resultView_.imgViewStar1.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k2StarEasy < self.steps && self.steps < k1StarEasy) {
        // 2 star
        resultView_.imgViewStar1.image = [UIImage imageNamed:@"star2.png"];
        resultView_.imgViewStar2.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k3StarEasy < self.steps && self.steps < k2StarEasy) {
        // 3 star
        resultView_.imgViewStar1.image = [UIImage imageNamed:@"star2.png"];
        resultView_.imgViewStar2.image = [UIImage imageNamed:@"star2.png"];
        resultView_.imgViewStar3.image = [UIImage imageNamed:@"star2.png"];
    }
    
    [[KGModal sharedInstance] showWithContentView:resultView_ andAnimated:YES];
    [resultView_.btnTop addAction:^(UIButton *btn) {
        [[KGModal sharedInstance] hideAnimated:YES];
        [self dismissViewControllerAnimated:YES completion:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [resultView_.btnPlayAgain addAction:^(UIButton *btn) {
        [[KGModal sharedInstance] hideAnimated:YES];
        [self restart:nil];
    } forControlEvents:UIControlEventTouchUpInside];
}
@end
